package article_management;
import java.sql.*;
public class EstablishConn {
	private Connection con;
	public EstablishConn(){
		loadingDriver("");
		setConnection("","","");
	}
	/*
	 * Constructor initialized by driver name , url , username , password
	 * */
	public EstablishConn(String driverName,String url,String user,String pwd){	
		loadingDriver(driverName);
		setConnection(url,user,pwd);
	}
	/*
	 *  Loading kind of database driver for connecting 
	 *  	-return true if loading driver success
	 * 		-return false if loading driver unsuccess
	 * */
	public boolean loadingDriver(String driverName){
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) { 
			return false;
		}
		return true;
	}
	/*
	 *   Connecting to database if loading driver success
	 *  	-return true if connected database success
	 * 		-return false if connected database unsuccess
	 * */
	public boolean setConnection(String url,String user,String pwd){
		try {
			con=DriverManager.getConnection(url,user,pwd);
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
	/*
	 *   Connecting to database if loading driver success
	 *  	-return true if connected database success
	 * 		-return false if connected database unsuccess
	 * */
	public boolean closeCon(){
		if(con!=null){
			try {
				con.close();
			} catch (SQLException e) {
				return false;
			}
		}
		return true;
	}

	public Connection getCon() {
		return con;
	}

	public void setCon(Connection con) {
		this.con = con;
	}
}
